﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using WebService.VO;
using System.Data.SqlClient;
using System.Data;

namespace WebService.DAO
{
    public class UserDAO
    {
        private dbConnection conn;
        /// <summary>
        /// khởi tạo đối tượng xử lý dl
        /// </summary>
        public UserDAO()
        {
            conn = new dbConnection();
        }

        public bool login(UserVO user)
        {
            string query = "SELECT count(*) FROM tbl_user WHERE username = @user AND password = @password";

            IDictionary<string, string> d = new Dictionary<string, string>();
            d.Add("@user", user.Username);
            d.Add("@password", user.Password);

            int kq=conn.ExecuteScalar(query, d, false);
            if(kq>0)
                return true;
            return false;
        }

        public bool updatePassword(UserVO user, string newPassword)
        {
            string query = "SELECT count(*) FROM tbl_user WHERE username = @user AND password = @password";

            IDictionary<string, string> d = new Dictionary<string, string>();
            d.Add("@user", user.Username);
            d.Add("@password", user.Password);

            int kq = conn.ExecuteScalar(query, d, false);
            if (kq==0)
                return false;
            query = "UPDATE tbl_user SET password=@password WHERE user=@user";

            d = new Dictionary<string, string>();
            d.Add("@user", user.Username);
            d.Add("@password", newPassword);
            return true;
        }
    }
}